import * as koaRouter from 'koa-router';
import { Config } from './app.config';
import { TestHandler } from './biz/test';
import { File } from './common/file';
import { ResponsePackage } from './common/http.response.package';
import { CheckHandler } from './dal/handler/check';
import { DictionaryItemHandler } from './dal/handler/dictionaryitem';
import { LogisticsHandler } from './dal/handler/logistics';
import { ProjectHandler } from './dal/handler/project';
import { SupplierHandler } from './dal/handler/supplier';
const router = koaRouter();
const file = new File();
const test = new TestHandler();
const diHandler = new DictionaryItemHandler();
const checkHandler = new CheckHandler();
const loHandler = new LogisticsHandler();
const projectHandler = new ProjectHandler();
const supplierHandler = new SupplierHandler();
// tslint:disable:no-shadowed-variable
router.get('/', async (ctx, next) => {
    ctx.response.body = ResponsePackage.toResponse('test');
});

router.post('/file/upload', async (ctx, next) => {
    const flag = file.upload(ctx, Config.UPLOAD_PATH);
    const msg = flag.length > 0 ? flag : 'upload error, please check logs';
    ctx.response.body = ResponsePackage.to({ flag: flag.length > 0, output: msg });
});

router.get('/file/download', async (ctx, next) => {
    file.download(ctx, Config.UPLOAD_PATH);
});

router.get('/voice', async (ctx, next) => {
    const query = ctx.request.query;
    const result = await test.readPCM();
    ctx.response.body = ResponsePackage.to(result);
});

router.get('/voicer', async (ctx, next) => {
    const query = ctx.request.query;
    const result = await test.readRemote();
    ctx.response.body = ResponsePackage.to(result);
});

router.get('/compose', async (ctx, next) => {
    const query = ctx.request.query;
    const result = await test.convertAudio();
    ctx.response.body = ResponsePackage.to(result);
});

router.post('/dictionary', async (ctx, next) => {
    const result = await diHandler.insert(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.put('/dictionary', async (ctx, next) => {
    const result = await diHandler.update(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.delete('/dictionary', async (ctx, next) => {
    const result = await diHandler.delete(ctx.request.query);
    ctx.response.body = ResponsePackage.to(result);
});

router.get('/dictionary', async (ctx, next) => {
    const result = await diHandler.query(ctx.request.query);
    ctx.response.body = ResponsePackage.to(result);
});

router.post('/check', async (ctx, next) => {
    const result = await checkHandler.insert(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.put('/check', async (ctx, next) => {
    const result = await checkHandler.update(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.delete('/check', async (ctx, next) => {
    const result = await checkHandler.delete(ctx.request.query);
    ctx.response.body = ResponsePackage.to(result);
});

router.get('/check', async (ctx, next) => {
    const result = await checkHandler.query();
    ctx.response.body = ResponsePackage.to(result);
});

router.post('/logistics', async (ctx, next) => {
    const result = await loHandler.insert(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.put('/logistics', async (ctx, next) => {
    const result = await loHandler.update(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.delete('/logistics', async (ctx, next) => {
    const result = await loHandler.delete(ctx.request.query);
    ctx.response.body = ResponsePackage.to(result);
});

router.get('/logistics', async (ctx, next) => {
    const result = await loHandler.query();
    ctx.response.body = ResponsePackage.to(result);
});

router.post('/project', async (ctx, next) => {
    const result = await projectHandler.insert(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.put('/project', async (ctx, next) => {
    const result = await projectHandler.update(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.delete('/project', async (ctx, next) => {
    const result = await projectHandler.delete(ctx.request.query);
    ctx.response.body = ResponsePackage.to(result);
});

router.get('/project', async (ctx, next) => {
    const result = await projectHandler.query();
    ctx.response.body = ResponsePackage.to(result);
});

router.post('/supplier', async (ctx, next) => {
    const result = await supplierHandler.insert(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.put('/supplier', async (ctx, next) => {
    const result = await supplierHandler.update(ctx.request.body);
    ctx.response.body = ResponsePackage.to(result);
});

router.delete('/supplier', async (ctx, next) => {
    const result = await supplierHandler.delete(ctx.request.query);
    ctx.response.body = ResponsePackage.to(result);
});

router.get('/supplier', async (ctx, next) => {
    const result = await supplierHandler.query();
    ctx.response.body = ResponsePackage.to(result);
});

export { router as Router };
